﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using GSM.BusinessLayer;
using GSM.BusinessLayer.DataLayer;
using CRM_SMS_MARKETING.DAL;
using CRM_SMS_MARKETING.BL;

namespace CRM_SMS_MARKETING.PL.Customer_Care
{
    public partial class frmBirthdayCustomer : DevExpress.XtraEditors.XtraForm
    {
        List<GSM.BusinessLayer.Customer> ListCustomer;
        CustomerSql customersql;
        UserAccountSql userAccountSql;
        BLEvent blEvent;
        int n;
        List<GSM.BusinessLayer.Customer> ListSelectedCustomer;
        List<GSM.BusinessLayer.Customer> ListUnSelectedCustomer;
        List<GSM.BusinessLayer.Customer> ListFemale;
        List<GSM.BusinessLayer.Customer> ListMale;
        List<GSM.BusinessLayer.Customer> ListUser;
        string formName;
        public frmBirthdayCustomer(string formname,List<GSM.BusinessLayer.Customer> listCustomer)
        {
            InitializeComponent();
            formName = formname;
            blEvent = new BLEvent();
            ListCustomer = new List<GSM.BusinessLayer.Customer>();
            ListCustomer = blEvent.GetListCustomerWithChecked(listCustomer);
     
            customersql = new CustomerSql();
            userAccountSql = new UserAccountSql();
            ListUnSelectedCustomer = new List<GSM.BusinessLayer.Customer>();
            ListSelectedCustomer = new List<GSM.BusinessLayer.Customer>();
            ListMale = new List<GSM.BusinessLayer.Customer>();
            ListFemale = new List<GSM.BusinessLayer.Customer>();
            lookUpUser.Properties.DataSource = userAccountSql.SelectAllUserName();
            lookUpUser.Properties.DisplayMember = "UserName";
            lookUpUser.Properties.ValueMember = "UserName";
        }


        private void frmBirthdayCustomer_Load(object sender, EventArgs e)
        {
            
            n = ListCustomer.Count;
            gridControlCustomer.DataSource = ListCustomer;
            List<string> ListCity = new List<string>();
            ListCity = (List<string>)customersql.GetListCity();
            for (int i = 0; i < ListCity.Count; i++)
            {
                comboBoxEditCity.Properties.Items.Add(ListCity[i]);
            }
        }

        private void lookUpUser_EditValueChanged(object sender, EventArgs e)
        {
            GetListWithAllCondition(comboBoxEditGender.Text, comboBoxEditCity.Text, lookUpUser.EditValue.ToString());
        }

        private void checkEditSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEditSelectAll.Checked == true)
            {
                //groupControlGender.Enabled = false;
                //groupControlLocation.Enabled = false;
                //lookUpUser.Enabled = false;
                //checkEditSelectUser.Enabled = false;
                //gridControlCustomer.DataSource = ListCustomer;
                int m = gridViewCustomer.RowCount;

                for (int i = 0; i < m; i++)
                {
                    gridViewCustomer.SetRowCellValue(i, colChoose, true);
                    ListCustomer[i].Deleted = true;
                }
            }
            else
            {
                int m = gridViewCustomer.RowCount;
                //gridControlCustomer.DataSource = ListCustomer;

                for (int i = 0; i < m; i++)
                {
                    gridViewCustomer.SetRowCellValue(i, colChoose, false);
                    ListCustomer[i].Deleted = false;

                }
                //groupControlGender.Enabled = true;
                //groupControlLocation.Enabled = true;
                //lookUpUser.Enabled = true;
                //checkEditSelectUser.Enabled = true;

            }
        }

        private void checkEditMale_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEditMale.Checked == true)
            {
                int n = gridViewCustomer.RowCount;
                for (int i = 0; i < n; i++)
                {
                    if ((bool)gridViewCustomer.GetRowCellValue(i, colGender) == true)
                    {
                        gridViewCustomer.SetRowCellValue(i, colChoose, true);
                        ListCustomer[i].Deleted = true;
                    }
                }
            }
            else
            {
                int n = gridViewCustomer.RowCount;
                for (int i = 0; i < n; i++)
                {
                    if ((bool)gridViewCustomer.GetRowCellValue(i, colGender) == true)
                    {
                        gridViewCustomer.SetRowCellValue(i, colChoose, false);
                        ListCustomer[i].Deleted = false;
                    }
                }
            }
        }

        private void checkEditFemale_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEditFemale.Checked == true)
            {
                int n = gridViewCustomer.RowCount;
                for (int i = 0; i < n; i++)
                {
                    if ((bool)gridViewCustomer.GetRowCellValue(i, colGender) == false)
                    {
                        gridViewCustomer.SetRowCellValue(i, colChoose, true);
                        ListCustomer[i].Deleted = true;
                    }

                }
            }
            else
            {
                int n = gridViewCustomer.RowCount;
                for (int i = 0; i < n; i++)
                {
                    if ((bool)gridViewCustomer.GetRowCellValue(i, colGender) == false)
                    {
                        gridViewCustomer.SetRowCellValue(i, colChoose, false);
                        ListCustomer[i].Deleted = false;
                    }

                }
            }
        }

        private void checkEditSelectUser_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEditSelectUser.Checked == true)
            {
                int n = gridViewCustomer.RowCount;
                for (int i = 0; i < n; i++)
                {
                    gridViewCustomer.SetRowCellValue(i, colChoose, true);
                    ListCustomer[i].Deleted = true;
                }
            }
            else
            {
                int n = gridViewCustomer.RowCount;
                for (int i = 0; i < n; i++)
                {
                    gridViewCustomer.SetRowCellValue(i, colChoose, false);
                    ListCustomer[i].Deleted = true;

                }
            }
        }
        private void GetListWithAllCondition(string Gender, string City, string User)
        {
            List<GSM.BusinessLayer.Customer> list = new List<GSM.BusinessLayer.Customer>();

            #region Truong hop toan bo deu rong
            if (Gender == "" && City == "" && User == "")
            {
                gridControlCustomer.DataSource = ListCustomer;
            }
            else
            #endregion

                #region Truong hop Gender!= rong
                if (Gender != "" && City == "" && User == "")
                {
                    ListMale = new List<GSM.BusinessLayer.Customer>();
                    ListFemale = new List<GSM.BusinessLayer.Customer>();

                    if (comboBoxEditGender.Text == "Male")
                    {


                        for (int i = 0; i < n; i++)
                        {
                            if (ListCustomer[i].Gender == true)
                            {
                                ListMale.Add(ListCustomer[i]);
                            }
                        }
                        if (ListMale.Count != 0)
                        {
                            gridControlCustomer.DataSource = ListMale;
                        }
                        else
                        {
                            gridControlCustomer.DataSource = null;
                        }

                    }
                    else if (comboBoxEditGender.Text == "Female")
                    {

                        int n = ListCustomer.Count;

                        for (int i = 0; i < n; i++)
                        {
                            if (ListCustomer[i].Gender == false)
                            {
                                ListFemale.Add(ListCustomer[i]);
                            }
                        }
                        if (ListFemale.Count != 0)
                        {
                            gridControlCustomer.DataSource = ListFemale;
                        }
                        else
                        {
                            gridControlCustomer.DataSource = null;
                        }


                    }
                }
                else
                #endregion

                    #region Truong hop City!= rong
                    if (Gender == "" && City != "" && User == "")
                    {
                        List<GSM.BusinessLayer.Customer> ListCity = new List<GSM.BusinessLayer.Customer>();

                        for (int i = 0; i < n; i++)
                        {
                            if (ListCustomer[i].City == comboBoxEditCity.Text)
                            {
                                ListCity.Add(ListCustomer[i]);
                            }
                        }
                        if (ListCity.Count != 0)
                        {
                            gridControlCustomer.DataSource = ListCity;
                        }
                        else
                        {
                            gridControlCustomer.DataSource = null;
                        }


                    }
                    else
                    #endregion

                        #region Truong hop User!=Rong
                        if (Gender == "" && City == "" && User != "")
                        {
                            if (lookUpUser.EditValue.ToString() == "All")
                            {
                                gridControlCustomer.DataSource = ListCustomer;
                            }
                            else
                            {
                                List<GSM.BusinessLayer.Customer> ListUser = new List<GSM.BusinessLayer.Customer>();


                                for (int i = 0; i < n; i++)
                                {
                                    if (ListCustomer[i].User == lookUpUser.EditValue.ToString())
                                    {
                                        ListUser.Add(ListCustomer[i]);
                                    }
                                }
                                if (ListUser.Count != 0)
                                {
                                    gridControlCustomer.DataSource = ListUser;
                                }
                                else
                                {

                                    gridControlCustomer.DataSource = null;
                                }

                            }
                        }
                        else
                        #endregion

                            #region Truong hop Gender va City != rong
                            if (Gender != "" && City != "" && User == "")
                            {
                                List<GSM.BusinessLayer.Customer> ListCity = new List<GSM.BusinessLayer.Customer>();
                                ListMale = new List<GSM.BusinessLayer.Customer>();
                                ListFemale = new List<GSM.BusinessLayer.Customer>();
                                for (int i = 0; i < n; i++)
                                {
                                    if (ListCustomer[i].City == comboBoxEditCity.Text)
                                    {
                                        ListCity.Add(ListCustomer[i]);
                                    }
                                }
                                if (ListCity.Count != 0)
                                {
                                    if (Gender == "Male")
                                    {
                                        int CityCount = ListCity.Count;
                                        for (int i = 0; i < CityCount; i++)
                                        {
                                            if (ListCity[i].Gender == true)
                                            {
                                                ListMale.Add(ListCity[i]);
                                            }
                                        }
                                        if (ListMale.Count != 0)
                                        {
                                            gridControlCustomer.DataSource = ListMale;
                                        }
                                        else
                                        {
                                            gridControlCustomer.DataSource = null;
                                        }
                                    }
                                    else if (Gender == "Female")
                                    {
                                        int CityCount = ListCity.Count;
                                        for (int i = 0; i < CityCount; i++)
                                        {
                                            if (ListCity[i].Gender == false)
                                            {
                                                ListFemale.Add(ListCity[i]);
                                            }
                                        }
                                        if (ListFemale.Count != 0)
                                        {
                                            gridControlCustomer.DataSource = ListFemale;
                                        }
                                        else
                                        {
                                            gridControlCustomer.DataSource = null;
                                        }

                                    }
                                }
                                else
                                {

                                    gridControlCustomer.DataSource = null;
                                }
                            }
                            else
                            #endregion

                                #region Truong hop Gender va User != rong
                                if (Gender != "" && City == "" && User != "")
                                {
                                    ListUser = new List<GSM.BusinessLayer.Customer>();
                                    ListMale = new List<GSM.BusinessLayer.Customer>();
                                    ListFemale = new List<GSM.BusinessLayer.Customer>();
                                    if (lookUpUser.EditValue.ToString() == "All")
                                    {
                                        ListUser = ListCustomer;
                                    }
                                    else
                                    {


                                        for (int i = 0; i < n; i++)
                                        {
                                            if (ListCustomer[i].User == lookUpUser.EditValue.ToString())
                                            {
                                                ListUser.Add(ListCustomer[i]);
                                            }
                                        }
                                    }
                                    if (ListUser.Count != 0)
                                    {
                                        int UserCount = ListUser.Count;
                                        #region Male

                                        if (Gender == "Male")
                                        {
                                            for (int i = 0; i < UserCount; i++)
                                            {
                                                if (ListUser[i].Gender == true)
                                                {
                                                    ListMale.Add(ListUser[i]);
                                                }
                                            }
                                            if (ListMale.Count != 0)
                                            {
                                                gridControlCustomer.DataSource = ListMale;
                                            }
                                            else
                                            {
                                                gridControlCustomer.DataSource = null;
                                            }
                                        }
                                        #endregion
                                        #region Female
                                        else
                                        {
                                            for (int i = 0; i < UserCount; i++)
                                            {
                                                if (ListUser[i].Gender == false)
                                                {
                                                    ListFemale.Add(ListUser[i]);
                                                }
                                            }
                                            if (ListFemale.Count != 0)
                                            {
                                                gridControlCustomer.DataSource = ListFemale;
                                            }
                                            else
                                            {
                                                gridControlCustomer.DataSource = null;
                                            }
                                        }
                                        #endregion

                                    }
                                    else
                                    {
                                        gridControlCustomer.DataSource = null;
                                    }


                                }
                                else
                                #endregion

                                    #region Truong hop City va User != rong
                                    if (Gender == "" && City != "" && User != "")
                                    {
                                        List<GSM.BusinessLayer.Customer> ListCity = new List<GSM.BusinessLayer.Customer>();
                                        ListUser = new List<GSM.BusinessLayer.Customer>();
                                        if (lookUpUser.EditValue.ToString() == "All")
                                        {
                                            ListUser = ListCustomer;
                                        }
                                        else
                                        {

                                            for (int i = 0; i < n; i++)
                                            {
                                                if (ListCustomer[i].User == lookUpUser.EditValue.ToString())
                                                {
                                                    ListUser.Add(ListCustomer[i]);
                                                }
                                            }
                                        }
                                        if (ListUser.Count != 0)
                                        {
                                            int UserCount = ListUser.Count;
                                            for (int i = 0; i < UserCount; i++)
                                            {
                                                if (ListUser[i].City == City)
                                                {
                                                    ListCity.Add(ListUser[i]);
                                                }
                                            }
                                            if (ListCity.Count != 0)
                                            {
                                                gridControlCustomer.DataSource = ListCity;
                                            }
                                            else
                                            {
                                                gridControlCustomer.DataSource = null;
                                            }
                                        }
                                        else
                                        {
                                            gridControlCustomer.DataSource = null;
                                        }


                                    }
                                    else
                                    #endregion

                                        #region Truong hop Gender , User va City != rong
                                        if (Gender != "" && City != "" && User != "")
                                        {
                                            ListMale = new List<GSM.BusinessLayer.Customer>();
                                            ListFemale = new List<GSM.BusinessLayer.Customer>();
                                            List<GSM.BusinessLayer.Customer> ListCity = new List<GSM.BusinessLayer.Customer>();
                                            ListUser = new List<GSM.BusinessLayer.Customer>();
                                            // Lay List Customer cua User
                                            if (lookUpUser.EditValue.ToString() == "All")
                                            {
                                                ListUser = ListCustomer;
                                            }
                                            else
                                            {

                                                for (int i = 0; i < n; i++)
                                                {
                                                    if (ListCustomer[i].User == lookUpUser.EditValue.ToString())
                                                    {
                                                        ListUser.Add(ListCustomer[i]);
                                                    }
                                                }
                                            }
                                            if (ListUser.Count != 0)
                                            {
                                                //Lay List City cua User
                                                int UserCount = ListUser.Count;
                                                for (int i = 0; i < UserCount; i++)
                                                {
                                                    if (ListUser[i].City == City)
                                                    {
                                                        ListCity.Add(ListUser[i]);
                                                    }
                                                }
                                                if (ListCity.Count != 0)
                                                {
                                                    int CityCount = ListCity.Count;

                                                    #region Male
                                                    if (Gender == "Male")
                                                    {
                                                        for (int i = 0; i < CityCount; i++)
                                                        {
                                                            if (ListUser[i].Gender == true)
                                                            {
                                                                ListMale.Add(ListUser[i]);
                                                            }
                                                        }
                                                        if (ListMale.Count != 0)
                                                        {
                                                            gridControlCustomer.DataSource = ListMale;
                                                        }
                                                        else
                                                        {
                                                            gridControlCustomer.DataSource = null;
                                                        }
                                                    }
                                                    #endregion

                                                    #region Female
                                                    else
                                                    {
                                                        for (int i = 0; i < UserCount; i++)
                                                        {
                                                            if (ListUser[i].Gender == false)
                                                            {
                                                                ListFemale.Add(ListUser[i]);
                                                            }
                                                        }
                                                        if (ListFemale.Count != 0)
                                                        {
                                                            gridControlCustomer.DataSource = ListFemale;
                                                        }
                                                        else
                                                        {
                                                            gridControlCustomer.DataSource = null;
                                                        }
                                                    }
                                                    #endregion
                                                }
                                                else
                                                {
                                                    gridControlCustomer.DataSource = null;
                                                }
                                            }
                                            else
                                            {
                                                gridControlCustomer.DataSource = null;
                                            }


                                        }

                                        #endregion


        }

        private void comboBoxEditCity_EditValueChanged(object sender, EventArgs e)
        {

            GetListWithAllCondition(comboBoxEditGender.Text, comboBoxEditCity.Text, lookUpUser.EditValue.ToString());
          
        }

        private void checkEditCity_CheckedChanged(object sender, EventArgs e)
        {
    
            if (checkEditCity.Checked == true)
            {
                int n = gridViewCustomer.RowCount;
                for (int i = 0; i < n; i++)
                {

                    gridViewCustomer.SetRowCellValue(i, colChoose, true);
                    ListCustomer[i].Deleted = true;

                }
            }
            else
            {
                int n = gridViewCustomer.RowCount;
                for (int i = 0; i < n; i++)
                {
                    gridViewCustomer.SetRowCellValue(i, colChoose, false);
                    ListCustomer[i].Deleted = true;

                }
            }
         
        }

        private void comboBoxEditView_EditValueChanged(object sender, EventArgs e)
        {
            GetListWithAllCondition(comboBoxEditGender.Text, comboBoxEditCity.Text, lookUpUser.EditValue.ToString());
        }
        private void comboBoxEditGender_EditValueChanged(object sender, EventArgs e)
        {
            GetListWithAllCondition(comboBoxEditGender.Text, comboBoxEditCity.Text, lookUpUser.EditValue.ToString());
            #region
            //ListMale = new List<GSM.BusinessLayer.Customer>();
            //ListFemale = new List<GSM.BusinessLayer.Customer>();
            //if (comboBoxEditGender.Text == "")
            //{
            //    gridControlCustomer.DataSource = ListCustomer;
            //}
            //else if (comboBoxEditGender.Text == "Male")
            //{


            //    for (int i = 0; i < n; i++)
            //    {
            //        if (ListCustomer[i].Gender == true)
            //        {
            //            ListMale.Add(ListCustomer[i]);
            //        }
            //    }
            //    gridControlCustomer.DataSource = ListMale;
            //}
            //else if (comboBoxEditGender.Text == "Female")
            //{



            //    for (int i = 0; i < n; i++)
            //    {
            //        if (ListCustomer[i].Gender == false)
            //        {
            //            ListFemale.Add(ListCustomer[i]);
            //        }
            //    }
            //    gridControlCustomer.DataSource = ListFemale;
            //}
            #endregion
        }

        private void btnOK_Click(object sender, EventArgs e)
        {


            List<GSM.BusinessLayer.Customer> ListFinalResult = new List<GSM.BusinessLayer.Customer>();
            for (int i = 0; i < n; i++)
            {
                if (ListCustomer[i].Deleted == true)
                {
                    ListFinalResult.Add(ListCustomer[i]);
                }
            }

            if (ListFinalResult.Count != 0)
            {
                if (formName == "frmBirthday")
                {
                    frmBirthday.ListCustomer = ListFinalResult;
                }
                else
                {
                    frmSpecialDay.ListCustomer = ListFinalResult;
                }
                this.Hide();
            }
            else if (MessageBox.Show("You do not choose any customer! Are you sure to complete this task?", "Confirm Complete", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                if (formName == "frmBirthday")
                {
                    frmBirthday.ListCustomer = ListFinalResult;

                }
                else
                {
                    frmSpecialDay.ListCustomer = ListFinalResult;

                }
                
                this.Hide();
            }
           
            

        }

        private void gridViewCustomer_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            for (int i = 0; i < n; i++)
            {
                if ((int)gridViewCustomer.GetRowCellValue(e.RowHandle, colID) == ListCustomer[i].CustomerID)
                {
                    ListCustomer[i].Deleted = (bool)e.Value;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

           
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            lookUpUser.EditValue = null;
            comboBoxEditCity.Text = "";
            comboBoxEditGender.Text = "";
            for (int i = 0; i < n; i++)
            {
                ListCustomer[i].Deleted = false;
            }
            gridControlCustomer.DataSource = ListCustomer;
        }

    }
}